package com.testing.redboy.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.testing.redboy.FirstActivity;
import com.testing.redboy.R;
import com.testing.redboy.bean.TimeBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class Time_Fragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String url;
    private ListView limit_lv;
    long mm = System.currentTimeMillis();


    public Time_Fragment() {
    }

    public static Time_Fragment newInstance(String param1, String url) {
        Time_Fragment fragment = new Time_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            url = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_time_, container, false);

        limit_lv = (ListView) v.findViewById(R.id.limit_lv);

        setData(url);

        TextView back = (TextView) v.findViewById(R.id.limit_back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });

        limit_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent0 = new Intent(getActivity(), FirstActivity.class);
                intent0.putExtra("fk", 7);
                startActivity(intent0);
            }
        });

        return v;
    }

    public void setData(String u) {
        RequestParams params = new RequestParams(u);
        x.http().get(params, new Callback.CommonCallback<String>() {

            private List<TimeBean.ProductlistBean> list;

            @Override
            public void onSuccess(String result) {
                Gson g = new Gson();
                TimeBean tb = g.fromJson(result, TimeBean.class);
                list = tb.productlist;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                limit_lv.setAdapter(new Mybs(getActivity(), list));
            }
        });
    }

    class Mybs extends BaseAdapter {

        Context context;
        List<TimeBean.ProductlistBean> list;

        public Mybs(Context context, List<TimeBean.ProductlistBean> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(context, R.layout.time_lv_item, null);
            ImageView image = (ImageView) convertView.findViewById(R.id.image);
            TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            TextView tv_yuanjia = (TextView) convertView.findViewById(R.id.tv_yuanjia);
            TextView tv_tejia = (TextView) convertView.findViewById(R.id.tv_tejia);
            final TextView tv_time = (TextView) convertView.findViewById(R.id.tv_time);

            String head = "http://120.27.126.103:8080/ECServer_D/";

            x.image().bind(image, head + list.get(position).pic);
            tv_name.setText(list.get(position).name);
            tv_yuanjia.setText("￥" + list.get(position).price);
            tv_tejia.setText("￥" + list.get(position).limitprice);

            //倒计时
            CountDownTimer countDownTimer = new CountDownTimer(mm, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mm = millisUntilFinished;
                    int day = 1;
                    int hour = (int) ((mm % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                    int minte = (int) ((mm % (1000 * 60 * 60)) / (1000 * 60));
                    int scound = (int) ((mm % (1000 * 60)) / 1000);
                    tv_time.setText(day + "天" + hour + "小时" + minte + "分钟" + scound + "秒");
                }

                @Override
                public void onFinish() {

                }
            };
            countDownTimer.start();

            return convertView;
        }
    }

}
